package com.googlecode.rulio.rendering.html;

import com.googlecode.rulio.predicates.OfTypePredicate;
import com.googlecode.rulio.rendering.Renderer;
import com.googlecode.totallylazy.Callable2;
import com.googlecode.totallylazy.Predicate;
import com.googlecode.totallylazy.predicates.AllPredicate;
import com.googlecode.totallylazy.predicates.AndPredicate;

class PredicateRenderers {
    public static Renderer<AndPredicate<?>> andPredicateRenderer(final Renderer<Object> renderer) {
        return new Renderer<AndPredicate<?>>() {
            public String render(AndPredicate<?> object) {
                return object.predicates().fold(null, new Callable2<String, Predicate, String>() {
                    public String call(String s, Predicate predicate) throws Exception {
                        return (s != null ? s + " and " : "") + renderer.render(predicate);
                    }
                });
            }
        };

    }


    public static Renderer<AllPredicate<?>> allPredicateRenderer() {
        return new Renderer<AllPredicate<?>>() {
            public String render(AllPredicate<?> object) {
                return "Any";
            }
        };

    }


    public static Renderer<OfTypePredicate> typedInstanceOfRenderer() {
        return new Renderer<OfTypePredicate>() {
            public String render(OfTypePredicate object) {
                return "ofType(" + object.type().getSimpleName() + ")";
            }
        };
    }
}
